<?php
if( !defined("IN_IA") )
{
    exit( "Access Denied" );
}
require(EWEI_SHOPV2_PLUGIN . "dividend/core/dividend_page_web.php");
class Price_EweiShopV2Page extends DividendWebPage
{
    public function main()
    {
        global $_W;
        global $_GPC;
        global $_S;
        $set = $this->model->getSet();
        $leveltype = $set["leveltype"];
        $list = pdo_fetchall("SELECT * FROM " . tablename("ewei_shop_performance_level") . " WHERE uniacid = :uniacid ORDER BY id DESC", array(":uniacid" => $_W["uniacid"]));

        include($this->template());
    }
    public function add()
    {
        $this->post();
    }
    public function edit()
    {
        $this->post();
    }
    protected function post()
    {
        global $_W;
        global $_GPC;
        $set = $this->model->getSet();
        $leveltype = $set["leveltype"];
        $id = trim($_GPC["id"]);

        $level = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_performance_level") . " WHERE id=:id and uniacid=:uniacid limit 1", array( ":id" => intval($id), ":uniacid" => $_W["uniacid"] ));

        if( $_W["ispost"] )
        {
            $data = array( "uniacid" => $_W["uniacid"], "levelname" => trim($_GPC["levelname"]), "commission" => trim(trim($_GPC["commission"]), "%"), "commissionmoney" => $_GPC["commissionmoney"], "downcount" => intval($_GPC["downcount"]), "agentcount" => intval($_GPC["agentcount"]), "withdraw" => $_GPC["withdraw"] );
            if( !empty($id) )
            {

                $updatecontent = "<br/>等级名称: " . $level["levelname"] . "->" . $data["levelname"] . "<br/>分红比例: " . $level["commission"];
                pdo_update("ewei_shop_performance_level", $data, array( "id" => $id, "uniacid" => $_W["uniacid"] ));
                plog("dividend.level.edit", "修改队长等级 ID: " . $id . $updatecontent);

            }
            else
            {
                pdo_insert("ewei_shop_performance_level", $data);
                $id = pdo_insertid();
                plog("dividend.level.add", "添加队长等级 ID: " . $id);
            }
            show_json(1, array( "url" => webUrl("dividend/price") ));
        }
        include($this->template());
    }
    public function delete()
    {
        global $_W;
        global $_GPC;
        $id = intval($_GPC["id"]);
        if( empty($id) )
        {
            $id = (is_array($_GPC["ids"]) ? implode(",", $_GPC["ids"]) : 0);
        }
        $items = pdo_fetchall("SELECT id,levelname FROM " . tablename("ewei_shop_performance_level") . " WHERE id in( " . $id . " ) AND uniacid=" . $_W["uniacid"]);
        foreach( $items as $item )
        {
            pdo_delete("ewei_shop_performance_level", array( "id" => $item["id"] ));
            plog("dividend.level.delete", "删除队长等级 ID: " . $id . " 等级名称: " . $level["levelname"]);
        }
        show_json(1);
    }
}
?>